package com.totoro.domain.key;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.totoro.domain.Resource;
import com.totoro.domain.Role;


/*
 * 定义Role-Resource关联表的主键实体
 */
@Embeddable
public class RoleResourceKey implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/*
	 * Field
	 */
	// 映射Resource为主键
	@ManyToOne(cascade = { CascadeType.ALL })
	@JoinColumn(name = "resc_id", referencedColumnName = "id")
	private Resource resource;// 资源

	// 映射Role为主键
	@ManyToOne(cascade = { CascadeType.ALL })
	@JoinColumn(name = "role_id", referencedColumnName = "id")
	private Role role;// 角色

	// 构造空的构造器
	public RoleResourceKey() {
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	// 主键实体类必须复写hashCode方法
	@Override
	public int hashCode() {
		return getResource().getNo().hashCode() * 13
				+ getRole().getNo().hashCode();
	}

	// 主键实体类必须复写equals方法
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj != null && this.getClass() == obj.getClass()) {
			RoleResourceKey target = (RoleResourceKey) obj;
			if (getResource().getNo().equals(target.getResource().getNo())
					&& getRole().getNo().equals(target.getRole().getNo()))
				return true;
		}
		return false;
	}

}
